package domain;

import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;

import pagemaker.ExternalProgramException;
import pagemaker.IPDFPageMaker;
import pagemaker.MuPDFPageMaker;

import java.awt.Graphics;

public class TestPDFPages {

    /**
     * @param args
     * @throws ExternalProgramException 
     * @throws IOException 
     */
    public static void main(String[] args) throws IOException, ExternalProgramException {
        MuPDFPageMaker.setProgram("C:\\utils\\mupdfpdfdraw"); // <= editar
        MuPDFPageMaker.setCacheDirectory("C:\\Temp\\pdfpages");     // <= editar
        IPDFPageMaker pageMaker1 = new MuPDFPageMaker(new File(args[0]));
        System.out.println("The document has " + pageMaker1.getNumberOfPages() + " pages.");
        ImageIcon ic = pageMaker1.getPage(1, 300);
        Image im = ic.getImage();
        BufferedImage bi1 =  imageToBufferedImage(im);
        ImageIO.write(bi1, "png", new File("xpto.png"));
    }
 
    public static BufferedImage imageToBufferedImage(Image im) {
       BufferedImage bi = new BufferedImage
          (im.getWidth(null),im.getHeight(null),BufferedImage.TYPE_INT_RGB);
       Graphics bg = bi.getGraphics();
       bg.drawImage(im, 0, 0, null);
       bg.dispose();
       return bi;
    }

}
